// pages/details/detail.js
import api from "../../utils/api.js"
const app = getApp()
Page({
  data: {
    productList:"",
    comment: "",
    brand: "",
    attribute: "",
    imgList: "",
    gallery: "",
    info: "",
    issue: "",
    id: '',
    banner: "",
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    quantity1: {
      quantity: 1,
      delStatus: 'disabled',
      addStatus: 'normal'
    },
    isLike: false,
    showDialog: false,
    animationData: {}
  },

  /* 输入框事件 */
  bindManual: function(e) {
    var count = this.data.quantity1.quantity;
    // 将数值与状态写回  
    this.setData({
      count: count
    });
  },
  /* 减数 */
  delCount: function(e) {
    console.log("刚刚您点击了减一");
    var count = this.data.quantity1.quantity;
    // 商品总数量-1
    if (count > 1) {
      count -= 1;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var delStatus = count <= 1 ? 'disabled' : 'normal';
    // 只有大于10件的时候，才能normal状态，否则disable状态  
    var addStatus = count >= 10 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      quantity1: {
        quantity: count,
        delStatus: delStatus,
        addStatus: addStatus
      }
    });
  },
  /* 加数 */
  addCount: function(e) {
    // console.log(e)
    console.log("加一");
    var count = this.data.quantity1.quantity;
    // 商品总数量-1  
    if (count < 100) {
      count += 1;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var delStatus = count <= 1 ? 'disabled' : 'normal';
    // 只有大于10件的时候，才能normal状态，否则disable状态  
    var addStatus = count >= this.data.info.number ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      quantity1: {
        quantity: count,
        delStatus: delStatus,
        addStatus: addStatus
      }
    });
  },
// 购物车弹出
  clickcart() {
    console.log(this)
    var _this = this;
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
      delay: 0
    })
    _this.animation = animation
    animation.translateY(300).step()
    _this.setData({
      animationData: animation.export(),
      showDialog: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      _this.setData({
        animationData: animation.export()
      })
    }.bind(_this), 50)
  },
  // 隐藏底部弹层
  hideModal: function() {
    var _this = this;
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    _this.animation = animation
    animation.translateY(300).step()
    _this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      _this.setData({
        animationData: animation.export(),
        showDialog: false
      })
    }.bind(this), 1000)
  },
  //加入购物车
  addCar: function(e) {
    let that =this
    console.log(e);
    let produce= e.currentTarget.dataset.id
    wx.request({
      url: api.shopadd,
      method: "POST",
      data: {
        goodsId: produce.goods_id,
        number: this.data.quantity1.quantity,
        productId: produce.id
      },
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      success(res) {
        console.log(res)
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          duration: 2000
        })
        that.hideModal()
      }
    })
  },
  // 收藏
  addLike() {
    this.setData({
      isLike: !this.data.isLike
    });
  },
  //关闭弹窗
  closeDialog: function() {
    console.info("关闭");
    this.setData({
      showDialog: false
    });
  },
  //点击购物弹出框
  toggleDialog() {
    // console.log(this)
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  //跳到购物车
  toShopList() {
    wx.switchTab({
      url: '../shopList/shopList',
    })
  },
  //页面预览
  preview(e) {
    // console.log(e)
    let current = e.currentTarget.dataset.src;
    let imgList = this.data.imgList
    wx.previewImage({
      current: imgList[current], //点击图片的链接
      urls: imgList,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //获得点击带到详情页的ID
  onLoad: function(options) {
    // console.log(this)
    let id = this.options.id;
    console.log(id)
    this.setData({
      id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  //业内阿贾克斯请求
  onShow: function() {
    let that = this
    wx.request({
      url: api.detail + "?id=" + this.data.id,
      success(res) {
        // console.log(res)
        let info = res.data.data.info
        let issue = res.data.data.issue
        let gallery = res.data.data.gallery
        let attribute = res.data.data.attribute
        let brand = res.data.data.brand
        let comment = res.data.data.comment.data
        let productList = res.data.data.productList[0]
        var imgList = [];
        gallery.forEach((el, index) => {
          imgList.push(el.img_url)
        })
        // console.log(simple_desc)
        that.setData({
          info,
          issue,
          gallery,
          imgList,
          attribute,
          brand,
          comment,
          productList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})